package com.hhkj.study.open;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.hhkj.study.admin.order.enums.OrderStatusEnum;
import com.hhkj.study.admin.order.model.podo.OrderItemDo;
import com.hhkj.study.admin.order.model.podo.OrderItemPo;
import com.hhkj.study.admin.order.service.base.OrderItemService;
import com.hhkj.study.api.user.common.handler.AccountSessionHandler;
import com.hhkj.study.api.user.model.res.ApiCustomerUserBaseRes;
import com.hhkj.study.api.user.sevice.biz.ApiUserCourseBizService;
import com.hhkj.study.common.model.req.CourseVideoReq;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.GlobalException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 处理脚本
 */
@RestController
@RequestMapping("/open/script")
public class OpenInnerApiController {


    @Resource
    private ApiUserCourseBizService apiUserCourseBizService;

    @Resource
    private OrderItemService orderItemService;

    /**
     * 订单下单后生成建培圈账号
     * @return
     */
    @PostMapping("/jpq/account")
    public AjaxResult jpqAccount(@RequestBody List<Long> orderItemIdList) {
        List<OrderItemPo> orderItemPoList = getOrderItemPoList(orderItemIdList);
        return AjaxResult.success(apiUserCourseBizService.userCourseGenerateOutSystemAccount(BeanUtil.copyToList(orderItemPoList, OrderItemDo.class)));
    }

    /**
     * 生成学习记录
     * @param orderItemIdList
     * @return
     */
    @PostMapping("/order/studyRecord")
    public AjaxResult generateStudyRecord(@RequestBody List<Long> orderItemIdList) {
        List<OrderItemPo> orderItemPoList = getOrderItemPoList(orderItemIdList);
        return AjaxResult.success(apiUserCourseBizService.userCourseStudyRecordSave(BeanUtil.copyToList(orderItemPoList, OrderItemDo.class)));
    }


    /**
     * 给课程增加视频
     * @return
     */
    @PostMapping("/add/video/course")
    public AjaxResult addCourseVideo(@Validated @RequestBody CourseVideoReq req) {
        return AjaxResult.success(apiUserCourseBizService.addCourseVideo(req));
    }



    private List<OrderItemPo> getOrderItemPoList(List<Long> orderItemIdList) {
        OrderItemDo orderItemDo = new OrderItemDo();
        orderItemDo.setStatus(OrderStatusEnum.PAID.getCode());
        if (CollectionUtil.isNotEmpty(orderItemIdList)) {
            orderItemDo.setOrderItemIdList(orderItemIdList);
        } else {
            ApiCustomerUserBaseRes currentAccount = AccountSessionHandler.getCurrentAccount();
            if (ObjectUtil.isNull(currentAccount)) {
                throw new GlobalException("未传子订单id时需要用户登录之后获取信息才能创建账号");
            }
            orderItemDo.setBuyerId(currentAccount.getId());
        }
        return orderItemService.listPoByDo(orderItemDo);
    }
}
